package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ConfigBean;
import com.koron.css2.baseConfig.mapper.ConfigMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 全局参数-编辑
 * @author 李攀
 */
public class ConfigUpdate implements ServerInterface {

	private static Logger logger = LogManager.getLogger(ConfigUpdate.class);

	@Override
	@ValidationKey(clazz = ConfigBean.class,method = "update")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			ConfigMapper mapper = factory.getMapper(ConfigMapper.class);
			ConfigBean bean = JsonUtils.objectToPojo(req.getData(), ConfigBean.class);
			// 校验字段重复
			if (mapper.check2("config_name", bean.getConfigName(), bean.getConfigId()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "参数名称：" + bean.getConfigName() + "的信息已存在。",
						void.class);
			}
			if (StringUtils.isNullOrEmpty(bean.getConfigId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id为空", void.class);
			}
			bean.setUpdateName(userInfo.getUserInfo().getName());
			bean.setUpdateTime(CommonUtils.getCurrentTime());
			mapper.updateConfig(bean);
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}
}